<?php
declare (strict_types=1);

namespace app\common\model;

class Footprint extends Base
{
    protected $name = 'footprint';
    
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile,photo');
    }
    public static function getTakeOverTableList($that, $param, $filed = '*', $count = 0)
    {
        try {
            foreach ($that as &$v) {
                $product = Product::where('id',$v['product_id'])->field('id,title,icon')->find();
                $html = '<a href="javascript:;" class="openShowImg" data-value="' . $product['icon'] . '"><img src="' . $product['icon'] . '" style="width: 20px; height:20px;border-radius: 5px;margin-right: 5px"/></a>';
                $v->product = '<p>' . $html . $product['title'] . "</p>";
            }
            return ['code' => 0, 'data' => $that, 'count' => $count];
        } catch (\Exception $e) {
            return [];
        }
    }
    //添加足迹
    public static function addFootprint($id,$user){
        parent::startTrans();
        try {
            $find=parent::where(['product_id'=>$id,'uid'=>$user->id])->whereTime('create_time','today')->find();
            if(empty($find)){
                $data['create_time']=time();
                $data['uid']=$user->id;
                $data['product_id']=$id;
                parent::create($data);
            }
            parent::commit();
            return ['code' => 1];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
